package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.utils.DateUtil;

public class BroadOrderActivity extends AppCompatActivity implements View.OnClickListener {
    private final static String ORDER_ACTION = "com.example.myapplication.order";
    private TextView tv_order_standard;
    private CheckBox checkbox_broad;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broad_order);

        checkbox_broad = findViewById(R.id.checkbox_broad);
        tv_order_standard = findViewById(R.id.tv_order_standard);
        findViewById(R.id.bnt_send_order).setOnClickListener(this);
    }
    /**
     * 点击事件发送广播
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.bnt_send_order){
            // 1.创建一个指定动作的意图
            Intent intent = new Intent(ORDER_ACTION);
            // 2.发送有序广播
            sendOrderedBroadcast(intent,null);
        }
    }
    private OrderAReceiver orderAReceiver;// 声明有序广播接收器A的实例
    /**
     * 定义一个有序广播的接收器A
     */
    private class OrderAReceiver extends BroadcastReceiver {
        // 一旦接收到有序广播A，马上触发接收器的onReceive方法
        @Override
        public void onReceive(Context context, Intent intent) {
            // 广播意图非空，且接头暗号正确
            if(intent != null && intent.getAction().equals(ORDER_ACTION)){
                String msg =  String.format("%s 接收器A收到一个有序广播\n", DateUtil.getNowTime());
                tv_order_standard.append(msg);
                if (checkbox_broad.isChecked()) {
                    abortBroadcast(); // 中断广播，此时后面的接收器无法收到该广播
                }
            }
        }
    }
    private OrderBReceiver orderBReceiver;// 声明有序广播接收器B的实例
    /**
     * 定义一个有序广播的接收器B
     */
    private class OrderBReceiver extends BroadcastReceiver {
        // 一旦接收到有序广播B，马上触发接收器的onReceive方法
        @Override
        public void onReceive(Context context, Intent intent) {
            // 广播意图非空，且接头暗号正确
            if(intent != null && intent.getAction().equals(ORDER_ACTION)){
                String msg =  String.format("%s 接收器B收到一个有序广播\n", DateUtil.getNowTime());
                tv_order_standard.append(msg);
                if (checkbox_broad.isChecked()) {
                    abortBroadcast(); // 中断广播，此时后面的接收器无法收到该广播
                }
            }
        }
    }

    /**
     * 注册和过滤A和B接收器
     */
    @Override
    protected void onStart() {
        super.onStart();
        //----------------创建一个有序广播的接收器A----------------//
        orderAReceiver = new OrderAReceiver();
        // 1.创建一个意图过滤器A，只处理ORDER_ACTION的广播
        IntentFilter filterA = new IntentFilter(ORDER_ACTION);
        // 2.设置过滤器A的优先级，数值越大优先级越高
        filterA.setPriority(8);
        // 3.注册接收器A，注册之后才能正常接收广播
        registerReceiver(orderAReceiver,filterA);
        //----------------创建一个有序广播的接收器B----------------//
        orderBReceiver = new OrderBReceiver();
        // 1.创建一个意图过滤器A，只处理ORDER_ACTION的广播
        IntentFilter filterB = new IntentFilter(ORDER_ACTION);
        // 2.设置过滤器A的优先级，数值越大优先级越高
        filterB.setPriority(10);
        // 3.注册接收器A，注册之后才能正常接收广播
        registerReceiver(orderBReceiver,filterB);

    }

    /**
     * 注销接收器
     */
    @Override
    protected void onStop() {
        super.onStop();
        // 注销接收器，注销之后就不再接收广播
        unregisterReceiver(orderAReceiver);
        unregisterReceiver(orderBReceiver);
    }
}